package com.laxia.interceptor;

import com.laxia.config.SpringContextHolder;
import com.laxia.constant.GlobalConstant;
import com.laxia.util.OptionalUtil;
import com.laxia.util.RedisUtil;
import com.laxia.util.SessionUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author weishuai
 * @version 1.0
 * @date 2020-04-20 00:15
 */
@Slf4j
public class UserSessionInterceptor extends HandlerInterceptorAdapter {

    private static RedisUtil redisUtil;

    static {
        redisUtil = SpringContextHolder.getBean(RedisUtil.class);
    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String token = request.getHeader(GlobalConstant.HEAD_AUTH_TOKEN);
        OptionalUtil.ofBlank(token).ifPresent(SessionUtil::addSessionUser);
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }


}
